<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class account_m extends CI_Model{
	
	public static $tokenPrefix = 'account_';
	
	public $m_CI = null;
	public function __construct()
	{
		parent::__construct();
		
		$this->m_CI = &get_instance();
		
		$this->m_CI->load->helper('account');
		
		
	}
	
	/**
	 * 获取用户数据
	 * @param array $data  // 筛选条件
	 */
	public function getAccount($data)
	{
		$sql	 = '';
		$implode = array();
		
		if (isset($data['id']) && !empty($data['id'])) {
			$implode[] = "id='" . intval($data['id']) . "'";
		}
		
		if (isset($data['name']) && !empty($data['name'])) {
			$implode[] = "name='" . $data['name'] . "'";
		}
		
		if (isset($data['email']) && !empty($data['email'])) {
			$implode[] = "email='" . $data['email'] . "'";
		}
		
		if (isset($data['pwd']) && !empty($data['pwd'])) {
			$implode[] = "pwd='" . createPwd($data['pwd']) . "'";
		
		}
		
		if ($implode) {
			$sql = "WHERE " . implode("AND ", $implode);
		}
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "account " . $sql);
		
		return $query->row_array();
	}
	
	public function setAccount($accountId,$data)
	{
		$toSet = array();
		
		$toWhere = array(
				'id'  => $accountId,
		);
		
		if (isset($data['name']) && !empty($data['name'])) {
			$toSet['name'] = $data['name'];
		}
		
		if (isset($data['pwd']) && !empty($data['pwd'])) {
			$toSet['pwd'] = $this->createPwd($data['pwd']);
		}
		
		if (isset($data['avatar']) && !empty($data['avatar'])) {
			$toSet['avatar'] = $data['avatar'];
		}
		
		if (isset($data['token']) && !empty($data['token'])) {
			$toSet['token'] = $data['token'];
		}
		
		if (empty($toSet))
		{
			return false;
		}

	    $sql =  $this->db->update_string(DB_PREFIX.'account',$toSet,$toWhere);
	    
	    return $this->db->query($sql);
		
	}
	
	/**
	 * 添加用户
	 * @param array $data 需要插入的用户名数据
	 * @return boolean
	 */
	public function addAccount($data)
	{
		$sql	 = '';
		$implode = array();
		
		if (isset($data['name']) && !empty($data['name'])) {
			$implode[] = "name='" . $data['name'] . "'";
		}

		if (isset($data['email']) && !empty($data['email'])) {
			$implode[] = "email='" . $data['email'] . "'";
		}
		else 
		{
			return false;
		}
		
		if (isset($data['pwd']) && !is_null($data['pwd'])) {
			$implode[] = "pwd='" . createPwd($data['pwd']) . "'";
		}
		else 
		{
			return false;
		}
		
		if (isset($data['avatar']) && !is_null($data['avatar'])) {
			$implode[] = "avatar='" . $data['avatar'] . "'";
		}

		if (isset($data['token']) && !empty($data['token'])) {
			$implode[] = "token='" . $data['type'] . "'";
		}
		else 
		{
			$implode[] = "token='" . createToken(self::$tokenPrefix) . "'";
		}
		if (isset($data['lastIP']) && !is_null($data['lastIP'])) {
			$implode[] = "lastIP='" . $data['lastIP'] . "'";
		}
		else 
		{
			$implode[] = "lastIP='" . $this->input->ip_address() . "'";
		}
		if (isset($data['lastTime']) && !empty($data['lastTime'])) {
			$implode[] = "lastTime='" . (int)$data['lastTime'] . "'";
		}
		else 
		{
			$implode[] = "lastTime='" . time() . "'";
		}
		
		if ($implode) {
			$sql = " " . implode(", ", $implode);
		}
		else {
			return false;
		}
		
		$this->db->query("INSERT " . DB_PREFIX . "account SET " . $sql);
		
		return $this->db->insert_id();
	}
	
	/* 
	 * @param string $p_email  邮箱
	 * @param string $p_pwd    密码
	 */
// 	public function login($p_email,$p_pwd)
// 	{
// 		$email = $p_email;
// 		$pwd   = createPwd($p_pwd);
		
// 		$query = $this->db->query("SELECT * FROM ".DB_PREFIX."account WHERE 'email='".$email." AND 'pwd' =".$pwd);
		
// 		return $query->row_array();
// 	} 
	
	/**
	 * 获取账户平台对应表的数据
	 * @param array $data
	 */
	public function getPlatfromList($data)
	{
		$sql	   = '';
		$sql_order = '';
		$implode = array();
		
		if (isset($data['accountID']) && !empty($data['accountID'])) {
			$implode[] = "accountID='" . intval($data['accountID']) . "'";
		}
		
		if (isset($data['platformID']) && !empty($data['platformID'])) {
			$implode[] = "platformID='" . intval($data['platformID']) . "'";
		}
		
		if (isset($data['token']) && !empty($data['token'])) {
			$implode[] = "token='" . $data['token'] . "'";
		}
		
		if (isset($data['retoken']) && !empty($data['retoken'])) {
			$implode[] = "retoken='" . $data['retoken'] . "'";
		}
		
		if (isset($data['openID']) && !empty($data['openID'])) {
			$implode[] = "openID='" . (int)$data['openID'] . "'";
		}
		
		if (isset($data['sort']) && isset($data['order']))
		{
			$order = 'ASC';
			if ($data['order'] == 'DESC')
			{
				$order = 'DESC';
			}
			
			if ($data['sort'] == 'addTime')
			{
				$sql_order .= 'ORDER BY addTime '.$order;
			}
			elseif ($data['sort'] == 'outTime')
			{
				$sql_order .= 'ORDER BY outTime '.$order;
			}
		}
		if ($implode) {
			$sql = "WHERE " . implode("AND ", $implode);
		}
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "account_platform " . $sql.$sql_order);

		return $query->result_array();
	}
	
	public function setPlatform($accountID,$platformID,$data)
	{
		$toSet   = array();
		
		$toWhere = array(
				'accountID'  => $accountID,
				'platformID' => $platformID,
				);
		
		if (isset($data['token']) && !empty($data['token'])) {
			$toSet['token'] = $data['token'];
		}
		
		if (isset($data['retoken']) && !empty($data['retoken'])) {
			$toSet['retoken'] = $data['retoken'];
		}
		
		if (isset($data['outTime']) && !empty($data['outTime'])) {
			$toSet['outTime'] = $data['outTime'];
		}
		
		if (isset($data['openID']) && !empty($data['openID'])) {
			$toSet['openID'] = $data['openID'];
		}
		
		if (empty($toSet))
		{
			return false;
		}
		
		$sql = $this->db->update_string(DB_PREFIX.'account_platform',$toSet,$toWhere);
		
		return $this->db->query($sql);
	}
	
	public function addPlatform($data)
	{
		$sql	 = '';
		$implode = array();
		
		if (isset($data['accountID']) && !empty($data['accountID'])) {
			$implode[] = "accountID='" . $data['accountID'] . "'";
		}
		
		if (isset($data['platformID']) && !empty($data['platformID'])) {
			$implode[] = "platformID='" . $data['platformID'] . "'";
		}
		
		if (isset($data['token']) && !is_null($data['token'])) {
			$implode[] = "token='" . $data['token'] . "'";
		}
		
		if (isset($data['retoken']) && !is_null($data['retoken'])) {
			$implode[] = "retoken='" . $data['retoken'] . "'";
		}
		
		if (isset($data['outTime']) && !empty($data['outTime'])) {
			$implode[] = "outTime='" . $data['outTime'] . "'";
		}
		
		if (isset($data['openID']) && !is_null($data['openID'])) {
			$implode[] = "openID='" . $data['openID'] . "'";
		}

		if (isset($data['addTime']) && !empty($data['addTime'])) {
			$implode[] = "addTime='" . (int)$data['addTime'] . "'";
		}
		
		if ($implode) {
			$sql = " " . implode(", ", $implode);
		}
		else {
			return false;
		}
		
		$this->db->query("INSERT " . DB_PREFIX . "account_platform SET " . $sql);
		
		return $this->db->insert_id();
	}
	
}